package com.linkage.educloud.ah.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.Child;
import com.linkage.educloud.ah.util.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Mr'Tang on 2018/9/5.
 * 自定义首页切换孩子view
 */

public class SwitchChildView extends LinearLayout {
    private Context mContext;
    private List<Child> list = new ArrayList<>();
    private NoScrollGridView grid_child;
    private TextView child_info;
    private OnClickListener onLongClickListener_shot, onLongClickListener_album;
    private static ChildAdapter adapter;
    private ChoosePicDialog dialog;

    public SwitchChildView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public SwitchChildView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_switchchild, this, true);
        grid_child = (NoScrollGridView) findViewById(R.id.grid_child);
        child_info = (TextView) findViewById(R.id.child_info);
        grid_child.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    public void setData(List<Child> data) {
        this.list = data;
        adapter = new ChildAdapter();
        grid_child.setAdapter(adapter);
        grid_child.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                dialog = new ChoosePicDialog(mContext);
                dialog.setShotListener(onLongClickListener_shot);
                dialog.setAlbumListener(onLongClickListener_album);
                dialog.show();
                return false;
            }
        });
        child_info.setText(data.get(0).getName() + "     "
                + data.get(0).getSchool() + "     "
                + data.get(0).getClassname());
    }

    public static int getChooseNum() {
        return adapter.select_item;
    }

    public void updata(List<Child> data) {
        this.list = data;
        adapter.notifyDataSetChanged();
    }

    public ChoosePicDialog getDialog() {
        return dialog;
    }

    private class ChildAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private int select_item = 0;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(final int arg0, View view, ViewGroup arg2) {
            final ViewHolder holder;
            if (view == null) {
                inflater = LayoutInflater.from(mContext);
                view = inflater.inflate(R.layout.item_child_avatar, null);
                holder = new ViewHolder();
                holder.user_img_small = (CircleImageView) view
                        .findViewById(R.id.user_face);
                holder.user_img_big = (CircleImageView) view
                        .findViewById(R.id.user_face_big);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            final Child accountChild = list.get(arg0);
            if (accountChild.getAvatar().startsWith("http")) {
                ImageLoader.loadAvatar(mContext, accountChild.getAvatar(), holder.user_img_small);
                ImageLoader.loadAvatar(mContext, accountChild.getAvatar(), holder.user_img_big);
            } else {
                ImageLoader.loadAvatar(mContext, Constants.IP + "/educloud_share/"
                        + accountChild.getAvatar(), holder.user_img_small);
                ImageLoader.loadAvatar(mContext, Constants.IP + "/educloud_share/"
                        + accountChild.getAvatar(), holder.user_img_big);
            }
            if (select_item == arg0) {
                holder.user_img_big.setVisibility(VISIBLE);
                holder.user_img_small.setVisibility(INVISIBLE);
            } else {
                holder.user_img_big.setVisibility(INVISIBLE);
                holder.user_img_small.setVisibility(VISIBLE);
            }
            holder.user_img_small.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    select_item = arg0;
                    notifyDataSetChanged();
                    child_info.setText(list.get(arg0).getName() + "     "
                            + list.get(arg0).getSchool() + "     "
                            + list.get(arg0).getClassname());
                }
            });
            return view;
        }
    }

    public class ViewHolder {
        private CircleImageView user_img_small, user_img_big;
    }

    public void setAlbumClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            onLongClickListener_album = onClickListener;
        }
    }

    public void setShotClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            onLongClickListener_shot = onClickListener;
        }
    }
}

